package com.hometeacher.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.hometeacher.api.annoation.UserLoginToken;
import com.hometeacher.api.model.Orders;
import com.hometeacher.api.model.StuList;
import com.hometeacher.api.model.TeacherList;
import com.hometeacher.api.model.User;
import com.hometeacher.api.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ZhaoYuan
 * @data 02/04/2020 21:38
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/user",method = RequestMethod.POST)
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    TokenService tokenService;

    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;

    @Autowired
    OrderService orderService;

    @PostMapping(value = "/login")
    public Object login(@RequestBody User user){
        JSONObject jsonObject=new JSONObject();
        User userForBase = userService.findByUsername(user);
        log.info("username {}  password {}",user.getUsername(),user.getPassword());
        if (userForBase==null) {
            jsonObject.put("message","登录失败,用户不存在");
            return jsonObject;
        }else {
            if (!userForBase.getPassword().equals(user.getPassword())) {
                jsonObject.put("message","登录失败,密码错误");
                return jsonObject;
            } else {
                userForBase.setPassword("******");
                String token = tokenService.getToken(userForBase);
                jsonObject.put("token",token);
                jsonObject.put("user",userForBase);
                jsonObject.put("message","succeed");
//              分辨是老师还是学生 个人信息传过去
                log.info("identity {}",userForBase.getIdentity());
                if (userForBase.getIdentity().equals("S")){
                    StuList stuListForBase = studentService.findByUser(user);
                    jsonObject.put("info",stuListForBase);
                }else {
                    log.info("teacher search");
                    TeacherList teacherListForBase = teacherService.findByUser(user);
                    jsonObject.put("info",teacherListForBase);
                }
                return jsonObject;
            }
        }
    }

    @PostMapping(value = "/register")
    public  Object register(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
        User userForBase = userService.findByUsername(user);
        if (userForBase == null){
            User user1 = userService.save(user);
            if ( user1 != null){
                jsonObject.put("data",user1);
                jsonObject.put("message","注册成功");
            }
        } else {
            jsonObject.put("message","注册失败,用户已经存在");
        }
        return jsonObject;
    }

//    @UserLoginToken
    @CrossOrigin
    @PostMapping(value = "/myOrders")
    public Object getOrders(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
        User userForBase = userService.findByUsername(user);
        log.info("username{} identity{}",user.getUsername(),user.getIdentity() );
        if (userForBase.getIdentity().equals("S")){
            StuList stuListForBase = studentService.findByUser(user);
            List<Orders> orders = orderService.findAllOrderByStu(stuListForBase.getStu_id());
            jsonObject.put("data",orders);
        }else {
            log.info("teacher search");
            TeacherList teacherListForBase = teacherService.findByUser(user);
            List<Orders> orders = orderService.findAllOrderByTeacher(teacherListForBase.getTeacherID());
            jsonObject.put("data",orders);
        }
        return jsonObject;
    }

    @PostMapping(value = "/confirm")
    public Object confirm(@RequestBody Orders orders){
        JSONObject jsonObject = new JSONObject();
        log.info("确认测试 {}",orders.getOrderStatus());
        orders.setOrderStatus(1);
        log.info("确认测试? {}",orders.getOrderId());
        orderService.confirm(orders);
        jsonObject.put("message","ok");

        return jsonObject;
    }


}
